﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSReporter.Utils;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;


namespace GlxRRSReporter.DailyReporter
{
    public class WeeklyReporterByCinema
    {
        private const int NGUYEN_DU_CINEMA_ID = 1001;
        private const int NGUYEN_TRAI_CINEMA_ID = 1002;
        private const int TAN_BINH_CINEMA_ID = 1003;
        //
        private const string SHEET_PER_CINEMA = "WR by Cinema";
        private const string SHEET_3 = "Sheet3";
        private static string TEMPLATE_DAILY_NAME = ".\\templates\\WeeklyReportByCinemaTemplate.xls";
        private static string OUT_MONTHLY_NAME = ".\\templates\\WeeklyReportByCinemaTemplate.xls";

        public static void init(string template, string outNameVal)
        {
            TEMPLATE_DAILY_NAME = template;
            OUT_MONTHLY_NAME = outNameVal;
        }

        public static void generateDailyReport(DateTime current,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmission,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBO,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailCO)
        {
            // load excel file
            Excel.Workbook workbook = ExportToExcel.openExcelWorkbooks(TEMPLATE_DAILY_NAME);
            Excel.Worksheet sheet = workbook.Worksheets[SHEET_PER_CINEMA];
            #region SET TITLE
            // week number
            sheet.get_Range("B5").Formula = current.CurrentWeekNumber().ToString();
            // current date
            sheet.get_Range("C5").Formula = current;//.Date.ToString();
            // start day of week
            sheet.get_Range("C7").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            #endregion
            //
            string[] column1001 = new string[] { "D", "E", "F"};
            string[] column1002 = new string[] { "G", "H", "I"};
            string[] column1003 = new string[] { "J", "K", "L"};
            string[] rowAdmission = new string[] { "7", "8", "9", "10", "11", "12", "13" };


            #region CHART ADMISSION DETAIL
            DateTime startOfWeek = current.StartOfWeek(DayOfWeek.Friday);
            int columnIdx = 0;
            int rowIdx = -1;
            string[] column = null;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailAdmission.Values)
            {
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    rowIdx = diff1.Days;
                    columnIdx = (detail.CinemaID - 1001) * 3;
                    //
                    if (detail.CinemaID == NGUYEN_DU_CINEMA_ID)
                    {
                        column = column1001;
                    }
                    else if (detail.CinemaID == NGUYEN_TRAI_CINEMA_ID)
                    {
                        column = column1002;
                    }
                    else if (detail.CinemaID == TAN_BINH_CINEMA_ID)
                    {
                        column = column1003;
                    }
                    string addr = column[0] + rowAdmission[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART BO DETAIL
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailBO.Values)
            {
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    rowIdx = diff1.Days;
                    columnIdx = (detail.CinemaID - 1001) * 3;
                    //
                    if (detail.CinemaID == NGUYEN_DU_CINEMA_ID)
                    {
                        column = column1001;
                    }
                    else if (detail.CinemaID == NGUYEN_TRAI_CINEMA_ID)
                    {
                        column = column1002;
                    }
                    else if (detail.CinemaID == TAN_BINH_CINEMA_ID)
                    {
                        column = column1003;
                    }
                    string addr = column[1] + rowAdmission[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART CO DETAIL
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailCO.Values)
            {
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    rowIdx = diff1.Days;
                    columnIdx = (detail.CinemaID - 1001) * 3;
                    //
                    if (detail.CinemaID == NGUYEN_DU_CINEMA_ID)
                    {
                        column = column1001;
                    }
                    else if (detail.CinemaID == NGUYEN_TRAI_CINEMA_ID)
                    {
                        column = column1002;
                    }
                    else if (detail.CinemaID == TAN_BINH_CINEMA_ID)
                    {
                        column = column1003;
                    }
                    string addr = column[2] + rowAdmission[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            // gen file name
            workbook.SaveAs(OUT_MONTHLY_NAME);
        }
    }
}
